function loading_in(containerId) {
    this.Id = FreeId("loadingBox");
    this.show = function () {
        $("#" + containerId).append('<div id="' + this.Id + '" style="text-align:center; width:130px; height:60px; background:url(' + UrlRoot + 'images/loading.gif) no-repeat;"></div>');
        var pos = ($.browser.msie && parseInt($.browser.version) <= 6) ? 'absolute' : 'fixed';
        var left = $("#" + containerId).width() / 2;
        var top = $("#" + containerId).height() / 2 + 152;
        $("#" + this.Id).css({ zIndex: 99999, position: pos, top: top, left: left });
    }
    this.hide = function () {
        $("#" + this.Id).remove();
    }
}
function FreeId(prefix) {
    if (document.getElementById(prefix) == null) return prefix;
    var i = 1;
    while (true) {
        var newId = prefix + "_" + i;
        if (document.getElementById(newId) == null)
            return newId;
        i++;
    }
}



/*Cate Home*/

var totalPageHome = 1;
var recordPPHome = 10;
var CateArticleIDHome = 0;
var urltemp = '';
var globalpageridhome;
var divcontenthomecate;
function LoadContentCateHome(cateid, page, url, isload, pageid, divcontentID) {
    $(".new_tap ul a").removeClass("fix");
    $("#div" + cateid).addClass("fix");
        $.ajax({
            type: "GET",
            url: UrlRoot + "Handler/Handler_Cate.ashx",
            data: { 'CateID': cateid, 'p': page },
            cache: false,
            dataType: "json",
            success: function (data) {
                if (data != null && data != "-1") {
                    var totalRecord = data.totalRecord;
                    CateArticleIDHome = cateid;
                    divcontenthomecate = divcontentID;
                    urltemp = url;
                    globalpageridhome = pageid;
                    totalPageHome = Math.ceil(totalRecord / recordPPHome);
                    buildContentHome(data.items, isload, totalPageHome, page, pageid, divcontentID);
                    $("#" + pageid).pager({ pagenumber: page, pagecount: totalPageHome, buttonClickCallback: PageClickCateHome, pagerId: pageid });
                    $("#divmore").attr("href", url);
                }
            }
        });
}
function buildContentHome(data, isload, totalPageHome, page, pageid, divcontentID) {
    var s = '';
    $.each(data, function (i, item) {
        s += '<div class="new_thumb">';
        s += '<div class="img">';
        s += '<a title="' + item.ftitle + '" href="' + item.url + '"><img src="' + item.image_link + '" width="250" height="160" /></a></div>';
        s += '<div class="noidung">';
        s += '<a href="' + item.url + '">' + item.title + '</a>';
        s += '<span>' + item.time + '</span>';
        s += '<p>' + item.summary + '</p>';
        s += '<div class="new_tab">';

        s += '<div style="float:right;width:132px;margin-top:50px;">';
        s += '<iframe src="//www.facebook.com/plugins/like.php?href=' + item.url + '&amp;width&amp;layout=button_count&amp;action=like&amp;show_faces=true&amp;share=true&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe>';
        s += '</div>';

        s += '<span class="' + item.classname + '">' + item.catename + '</span></div>';
        s += '</div>';
        s += '<div class="clear"></div>';
        s += '</div>';
        s += '<!--new_thumb-->';
    });
    if ((isload == true) && (totalPageHome > 2)) {
        s += "<a href=\"javascript:void(0)\" class=\"load_more\" onclick=\"LoadContentCateHome(" + CateArticleIDHome + ",2,'" + urltemp + "',false,'" + pageid + "','" + divcontentID + "')\">Xem thêm</a>";
        $('#' + pageid).hide();
    }
    else {
        $('#' + pageid).show();
    }

    if (page == 2) {
        $('#' + divcontentID).append(s);
        $(".load_more").hide();
    }
    else
        document.getElementById("" + divcontentID + "").innerHTML = s;
}
PageClickCateHome = function (pageclickednumber) {
    $("#" + globalpageridhome).pager({ pagenumber: pageclickednumber, pagecount: totalPageHome, buttonClickCallback: PageClickCateHome, pagerId: globalpageridhome });
    LoadContentCateHome(CateArticleIDHome, pageclickednumber, urltemp, false, globalpageridhome, divcontenthomecate);
}
/*End Cate Home*/

/*Load content other*/

var CateArticleID = 0;
var articleid = 0;
var p = 0;
var totalPage = 1;
var recordPP = 10;
var globalpageid;

function load_Content_OtherDetail(cate_id, page, ArticleId,pageid) {
    var loading = new loading_in("divotherdetail");
    loading.show();
    $.ajax({
        type: "GET",
        url: UrlRoot + "Handler/GetOhterArticle.ashx",
        data: {'CateID': cate_id,'p': page,'ArticleId':ArticleId },
        processData: true,
        dataType: "json",
        success: function (data) {
            loading.hide();
            if (data != null && data != "-1") {
                var totalRecord = data.totalRecord;
                CateArticleID = cate_id;
                articleid = ArticleId;
                globalpageid = pageid;
                totalPage = Math.ceil(totalRecord / recordPP);
                buildContent(data.items);                
                $("#pager").pager({ pagenumber: page, pagecount: totalPage, buttonClickCallback: PageClickOtherDetail, pageId: globalpageid });
            }
            else {
                $("#divotherdetail").hide();
            }

        }
    });
}
PageClickOtherDetail = function (pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: totalPage, buttonClickCallback: PageClick, pageId: globalpageid });
    load_Content_OtherDetail(CateArticleID, pageclickednumber, articleid);
}
function buildContent(data) {
    var s = '';
    $.each(data, function (i, item) {
        s += '<a title="' + item.ftitle + '" href="' + item.url + '"><span>' + item.time + '</span>' + item.title + '</a>';
    });
    s += '<div class="clear"></div>';
    document.getElementById("divotherdetail").innerHTML = s;
}
/*End Load content other*/



// Load Data Cate
var totalPageCate = 1;
var recordPPCate = 10;
var CateID = 0;
var globalpageid;

function load_Cate_content(cate_id, page,pageid) {
    $.ajax({
        type: "GET",
        url: UrlRoot + "Handler/Handler_Cate.ashx",
        data: { 'CateID': cate_id, 'p': page },
        processData: true,
        dataType: "json",
        success: function (data) {
            if (data != null && data != "-1") {
                var totalRecord = data.totalRecord;
                CateID = cate_id;
                globalpageid = pageid;
                totalPageCate = Math.ceil(totalRecord / recordPPCate);
                buildContentCate(data.items);
                $("#pager").pager({ pagenumber: page, pagecount: totalPageCate, buttonClickCallback: PageClick, pagerId: pageid });
            }
            else {
                $("#divcate").hide();
            }

        }
    });
}
PageClick = function (pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: totalPage, buttonClickCallback: PageClick, pagerId: globalpageid });
    load_Cate_content(CateID, pageclickednumber,globalpageid);
}
function buildContentCate(data) {
    var s = '';
    $.each(data, function (i, item) {
        s += '<div class="new_thumb">';
        s += '<div class="img">';
        s += '<a title="' + item.ftitle + '" href="' + item.url + '"><img src="' + item.image_link + '" width="250" height="160" /></a></div>';
        s += '<div class="noidung">';
        s += '<a href="' + item.url + '">' + item.title + '</a>';
        s += '<span>' + item.time + '</span>';
        s += '<p>' + item.summary + '</p>';
        s += '<div class="new_tab">';

        s += '<div style="float:right;width:132px;margin-top:50px;">';
        s += '<iframe src="//www.facebook.com/plugins/like.php?href=' + item.url + '&amp;width&amp;layout=button_count&amp;action=like&amp;show_faces=true&amp;share=true&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe>';
        s += '</div>';

        s += '<span class="' + item.classname + '">' + item.catename + '</span></div>';
        s += '</div>';
        s += '<div class="clear"></div>';
        s += '</div>';
        s += '<!--new_thumb-->';

    });
    document.getElementById("divcate").innerHTML = s;
}
// End Load Data Cate


// Load Photo,Video,FanArt

var totalPageCatePhoto = 1;
var recordPPCatePhoto = 9;
var globalpageidmedia;
function load(type,page,pageid) {
    if (type == 'photo') {
        $.ajax({
            type: "GET",
            url: UrlRoot + "Handler/Handler_photo.ashx",
            data: { 'action': type, 'p': page },
            processData: true,
            dataType: "json",
            success: function (data) {
                if (data != '' && data != null && data != '-1') {
                    var totalRecord = data.totalRecord;
                    globalpageidmedia = pageid;
                    totalPageCatePhoto = Math.ceil(totalRecord / recordPPCatePhoto);
                    buildphoto(data.items);
                    $("#pager").pager({ pagenumber: page, pagecount: totalPageCatePhoto, buttonClickCallback: PageClickPhoto, pageId: globalpageidmedia });
                    $(".group1").colorbox({ maxWidth: 1000 });
                }
            }
        });
    }
    else if (type == 'video') {
        $.ajax({
            type: "GET",
            url: UrlRoot + "Handler/Handler_clip.ashx",
            data: { 'action': type, 'p': page },
            processData: true,
            dataType: "json",
            success: function (data) {
                if (data != '' && data != null && data != '-1') {
                    var totalRecord = data.totalRecord;
                    globalpageidmedia = pageid;
                    totalPageCatePhoto = Math.ceil(totalRecord / recordPPCatePhoto);
                    buildvideo(data.items);
                    $("#pager").pager({ pagenumber: page, pagecount: totalPageCatePhoto, buttonClickCallback: PageClickVideo, pageId: globalpageidmedia });
                }
            }
        });
    }
    else if (type == 'fanart') {
        $.ajax({
            type: "GET",
            url: UrlRoot + "Handler/Handler_photo.ashx",
            data: { 'action': type, 'p': page },
            processData: true,
            dataType: "json",
            success: function (data) {
                if (data != '' && data != null && data != '-1') {
                    var totalRecord = data.totalRecord;
                    totalPageCatePhoto = Math.ceil(totalRecord / recordPPCatePhoto);
                    globalpageidmedia = pageid;
                    buildphoto(data.items);
                    $("#pager").pager({ pagenumber: page, pagecount: totalPageCatePhoto, buttonClickCallback: PageClickFanArt, pageId: globalpageidmedia });
                    $(".group1").colorbox({ maxWidth: 1000 });
                }
            }
        });
    }
}
function buildphoto(data) {
    var s = '';
    $.each(data, function (i, item) {
        s += '<li><a class="group1" href="' + item.image_link + '"><img src="' + item.image_link + '.250.160.cache"  width="250" height="160" /> <p style="height:25px;width:200px;">' + item.title + '</p></a></li>';
    });
    document.getElementById("divHVF").innerHTML = s;
}
PageClickPhoto = function (pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: totalPageCatePhoto, buttonClickCallback: PageClickPhoto, pageId: globalpageidmedia });
    load("photo",pageclickednumber,globalpageidmedia);
}
PageClickFanArt = function (pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: totalPageCatePhoto, buttonClickCallback: PageClickPhoto, pageId: globalpageidmedia });
    load("fanart", pageclickednumber, globalpageidmedia);
}
function buildvideo(data) {
    var s = '';
    $.each(data, function (i, item) {
        s += '<li><a href="' + item.url + '"><img src="' + item.ThumbNail + '"  width="250" height="160" /> <p style="height:25px;width:200px;">' + item.title + '</p></a></li>';
    });
    document.getElementById("divHVF").innerHTML = s;
}
PageClickVideo = function (pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: totalPageCatePhoto, buttonClickCallback: PageClickVideo, pageId: globalpageidmedia });
    load("video", pageclickednumber,globalpageidmedia);
}
// End Load Photo,Video,FanArt

// Load Data Search

var totalPageSearch = 1;
var recordPPSearch = 10;
var globalpageidsearch;

function load_news_contentSearch(key_word, page,pageid) {
    $.ajax({
        type: "GET",
        url: UrlRoot + "Handler/HandlerSearch.ashx",
        data: "keyword=" + key_word + "&p=" + page,
        processData: true,
        dataType: "json",
        success: function (data) {
            if (data != '' && data != null) {
                var totalRecord = data.totalRecord;
                totalPageSearch = Math.ceil(totalRecord / recordPPSearch);
                buildContentSearch(data.items);
                globalpageidsearch = pageid;
                $("#pager").pager({ pagenumber: page, pagecount: totalPageSearch, buttonClickCallback: PageClickSearch, pageId: globalpageidsearch });
            }
            else {
                $("#divSearch_top").html('');
            }

        }
    });
}

PageClickSearch = function (pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: totalPageSearch, buttonClickCallback: PageClickSearch, pageId: globalpageidsearch });
    load_news_contentSearch(key_word, pageclickednumber);
}

function buildContentSearch(data) {
    var s = '';
    $.each(data, function (i, item) {
        s += '<div class="new_thumb">';
        s += '<div class="img">';
        s += '<a title="' + item.ftitle + '" href="' + item.url + '"><img src="' + item.image_link + '" width="250" height="160" /></a></div>';
        s += '<div class="noidung">';
        s += '<a href="' + item.url + '">' + item.title + '</a>';
        s += '<span>' + item.time + '</span>';
        s += '<p>' + item.summary + '</p>';
        s += '<div class="new_tab">';

        s += '<div style="float:right;width:132px;margin-top:50px;">';
        s += '<iframe src="//www.facebook.com/plugins/like.php?href=' + item.url + '&amp;width&amp;layout=button_count&amp;action=like&amp;show_faces=true&amp;share=true&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe>';
        s += '</div>';

        s += '<span class="' + item.classname + '">' + item.catename + '</span></div>';
        s += '</div>';
        s += '<div class="clear"></div>';
        s += '</div>';
        s += '<!--new_thumb-->';

    });
    document.getElementById("divcate").innerHTML = s;
}
function LoadDataCate(index,cateid) {
    $(".new_tap ul a").removeClass("fix");
    $("#" + cateid).addClass("fix");
    if (index == 1) {
        $("#divtintuc").show();
        $("#divupdate").hide();
        $("#divsukien").hide();
        $("#divesport").hide();
        $("#divhalloween").hide();
    }
    if (index == 2) {
        $("#divtintuc").hide();
        $("#divupdate").show();
        $("#divsukien").hide();
        $("#divesport").hide();
        $("#divhalloween").hide();
    }
    if (index == 3) {
        $("#divtintuc").hide();
        $("#divupdate").hide();
        $("#divsukien").show();
        $("#divesport").hide();
        $("#divhalloween").hide();
    }
    if (index == 4) {
        $("#divtintuc").hide();
        $("#divupdate").hide();
        $("#divsukien").hide();
        $("#divesport").show();
        $("#divhalloween").hide();
    }
    if (index == 5) {
        $("#divtintuc").hide();
        $("#divupdate").hide();
        $("#divsukien").hide();
        $("#divesport").hide();
        $("#divhalloween").show();
    }
}

function LoadSearch()
{
    var keyWord = $("#txtkeyword").val();
    if ($("#txtkeyword").val() <= 0) {
        $("#txtkeyword").focus();
        return false;
    }
    window.location.href = UrlRoot + 'PageSearch.aspx?keySearch=' + keyWord;
}
$(document).ready(function () {
    $('#searchButton').click(function () {
        var search = $('#txtkeyword').val();
        window.location.href = UrlRoot + 'PageSearch.aspx?keySearch=' + search;
    });
    $('#txtkeyword').keypress(function (e) {
        if (e.which == 13) {//Enter key pressed
            $('#searchButton').click();//Trigger search button click event
        }
    });
});

//Load Article by Tag
var pagetag = 1;
var totalPageTag = 0;
function load_ArticlebyTag(ArticleID,page) {
    $.ajax({
        type: "GET",
        url: UrlRoot + "Handler/Handler_ArticleTag.ashx",
        data: "ArticleID=" + ArticleID + "&p=" + page,
        processData: true,
        dataType: "json",
        success: function (data) {
            if (data != '' && data != null) {
                totalRecordTag = data.totalRecord;
                totalPageTag = Math.ceil(totalRecordTag / 6);
                buildContentArticleTag(data.items,ArticleID);
            }
            else {
                $("#divTag").html('');
            }

        }
    });
}
function buildContentArticleTag(data, ArticleID) {
    var s = '';
    s += '<div class="clear"></div>';
    s += '<div class="new_title">';
    s += 'Có thể bạn quan tâm';
    s += '<a href="javascript:;" class="tenphai chon" onclick="LoadArticleTagPreNext(2,' + ArticleID + ',' + totalPageTag + ')"></a>';
    s += '<a href="javascript:;" class="tentrai" onclick="LoadArticleTagPreNext(1,' + ArticleID + ',' + totalPageTag + ')"></a>';
    s += '</div>';
    $.each(data, function (i, item) {
        if ((i+1) % 3 == 0 && i != 0) {

            s += '<div class="them2">';
            s += '<a title="' + item.ftitle + '" href="' + item.url + '"><img src="' + item.image_link + '" width="200" height="117" /></a> <a href="' + item.url + '">' + item.title + '</a>';
            s += '</div>';

        }
        else {
            s += '<div class="them1">';
            s += '<a title="' + item.ftitle + '" href="' + item.url + '"><img src="' + item.image_link + '" width="200" height="117" /></a> <a href="' + item.url + '">' + item.title + '</a>';
            s += '</div>';
        }
        if (((i+1) % 3 == 0 && i != 0) || (i == totalRecordTag-1))
        { 
            s += '<div class="clear"></div>';
        }
    });
			s += '<div class="clear"></div>';
    document.getElementById("divTag").innerHTML = s;
}

function LoadArticleTagPreNext(isnextpre,articleid,totalpage)
{
    //pre
    if (isnextpre == 1) {
        if (pagetag > 1)
            pagetag = pagetag - 1;
    }
    else {
        //next
        if (pagetag < totalpage)
            pagetag = pagetag + 1;
    }
    load_ArticlebyTag(articleid, pagetag)
}

//End Load Article by Tag





// Load Data SearchTag
var totalPageSearchTag = 1;
var recordPPSearchTag = 6;
var globalpageidsearchTag;

function load_news_contentSearchTag(TagId, page, pageid) {
    $.ajax({
        type: "GET",
        url: UrlRoot + "Handler/HandlerSearchTag.ashx",
        data: "TagId=" + TagId + "&page=" + page,
        processData: true,
        dataType: "json",
        success: function (data) {
            if (data != '' && data != null) {
                var totalRecord = data.totalRecord;
                totalPageSearchTag = Math.ceil(totalRecord / recordPPSearchTag);
                buildContentSearchTag(data.items);
                globalpageidsearchTag = pageid;
                $("#pager").pager({ pagenumber: page, pagecount: totalPageSearchTag, buttonClickCallback: PageClickSearchTag, pageId: globalpageidsearchTag });
            }
            else {
                $("#divSearch_top").html('');
            }

        }
    });
}

PageClickSearchTag = function (pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: totalPageSearch, buttonClickCallback: PageClickSearchTag, pageId: globalpageidsearch });
    load_news_contentSearchTag(TagId, pageclickednumber);
}

function buildContentSearchTag(data) {
    var s = '';
    $.each(data, function (i, item) {
        s += '<div class="new_thumb">';
        s += '<div class="img">';
        s += '<a title="' + item.ftitle + '" href="' + item.url + '"><img src="' + item.image_link + '" width="250" height="160" /></a></div>';
        s += '<div class="noidung">';
        s += '<a href="' + item.url + '">' + item.title + '</a>';
        s += '<span>' + item.time + '</span>';
        s += '<p>' + item.summary + '</p>';
        s += '<div class="new_tab">';

        s += '<div style="float:right;width:132px;margin-top:50px;">';
        s += '<iframe src="//www.facebook.com/plugins/like.php?href=' + item.url + '&amp;width&amp;layout=button_count&amp;action=like&amp;show_faces=true&amp;share=true&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe>';
        s += '</div>';

        s += '<span class="' + item.classname + '">' + item.catename + '</span></div>';
        s += '</div>';
        s += '<div class="clear"></div>';
        s += '</div>';
        s += '<!--new_thumb-->';

    });
    document.getElementById("divcate").innerHTML = s;
}
// End Load Data SearchTag
